﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class 个人 : MonoBehaviour
{
    public int m_n出生年份;
    public int m_n年龄
    {
        get
        {
            return (Manager.s_instance.m_n当前年代 - m_n出生年份);
        }
    }
    public float m_f死亡率;
    public float m_f我的死亡随机数;
    public float m_f生育率;
    public float m_f我的生育随机数;
    public Material m_material;

    // Start is called before the first frame update
    void Start()
    {
        m_material = GetComponent<MeshRenderer>().material;
    }

    private void FixedUpdate()
    {
        StartCoroutine(处理());
    }

    private IEnumerator 处理()
    {
        yield return new WaitForSeconds(0.1f);

        m_f死亡率 = Manager.s_instance.m_ac年龄死亡率.Evaluate(m_n年龄);
        m_f我的死亡随机数 = UnityEngine.Random.Range(0.0f, 1.0f);
        m_f生育率 = Manager.s_instance.m_ac年龄生育率.Evaluate(m_n年龄) * Manager.s_instance.m_ac年代_生育率系数.Evaluate(Manager.s_instance.m_n当前年代);
        m_f我的生育随机数 = UnityEngine.Random.Range(0.0f, 1.0f);

        if (m_f我的生育随机数 < m_f生育率)
        {
            m_material.color = Color.green;
            yield return new WaitForSeconds(0.3f);
            生育();
            m_material.color = Color.white;
        }
        else
        {
            yield return new WaitForSeconds(0.3f);
        }

        yield return new WaitForSeconds(0.1f);

        if (m_f我的死亡随机数 < m_f死亡率)
        {
            m_material.color = Color.red;
            yield return new WaitForSeconds(0.3f);
            死亡();
        }
    }

    public void 零岁()
    {
        StartCoroutine(零岁死亡判定());
    }

    private IEnumerator 零岁死亡判定()
    {
        m_f死亡率 = Manager.s_instance.m_ac年龄死亡率.Evaluate(m_n年龄);
        m_f我的死亡随机数 = UnityEngine.Random.Range(0.0f, 1.0f);

        yield return new WaitForSeconds(0.1f);

        if (m_f我的死亡随机数 < m_f死亡率)
        {
            m_material.color = Color.red;
            yield return new WaitForSeconds(0.3f);
            死亡();
        }
    }

    private void 生育()
    {
        Manager.s_instance.生育();
    }

    private void 死亡()
    {
        Destroy(gameObject);
    }
}
